package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.TbBaseInfo;
import com.ys.entity.finance.TcwFinancialTransactionTypeItem;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.storage.TccLocation;
import com.ys.entity.sys.TsUserChange;
import com.ys.mapper.finance.FinanceMapper;
import com.ys.mapper.finance.TcwFinancialTransactionTypeItemMapper;
import com.ys.mapper.hr.HrMapper;
import com.ys.mapper.finance.TcwFinancialTransactionTypeMapper;
import com.ys.service.finance.TcwFinancialTransactionTypeService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service("tcwFinancialTransactionTypeService")
public class TcwFinancialTransactionTypeServiceImpl implements TcwFinancialTransactionTypeService {
	private static final Logger logger = LoggerFactory.getLogger(TcwFinancialTransactionTypeServiceImpl.class);

	@Autowired
	private TcwFinancialTransactionTypeMapper tcwFinancialTransactionTypeMapper;

	@Autowired
	private TcwFinancialTransactionTypeItemMapper tcwFinancialTransactionTypeItemMapper;


	@Autowired
	private CheckUtil checkUtil;


	/*
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception {

		TcwFinancialTransactionType tcwFinancialTransactionType = JSON.parseObject(JSON.toJSONString(params), TcwFinancialTransactionType.class);
		String loginUserId = ShiroUtils.getUserId();
		TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
		tcwFinancialTransactionType.setStatus(1);
		String firstType = params.get("firstType").toString();
		String secondType = params.get("secondType").toString();
		if (firstType == null || secondType == null) {
			return R.error("请补充必填项");
		} else if (new TcwFinancialTransactionType().setFirstType(firstType).setSecondType(secondType).count() > 0) {
			return R.error("已新增过该数据，请重新组合");
		}
		tcwFinancialTransactionTypeMapper.insert(tcwFinancialTransactionType);

		return R.ok(AddDto.closeAndReload());
	}



	@Override
	public R edit(Map<String, Object> params) throws Exception {
		logger.info("申请修改传参:"+params);
		TcwFinancialTransactionType tcwFinancialTransactionType = JSON.parseObject(JSON.toJSONString(params), TcwFinancialTransactionType.class);
		tcwFinancialTransactionTypeMapper.update(tcwFinancialTransactionType);
		return R.ok("操作成功");
	}

	

	@Override
	@Transactional
	public R toDelByItem(Long id) throws Exception {
		
		TcwFinancialTransactionTypeItem tcwFinancialTransactionTypeItem = new TcwFinancialTransactionTypeItem();
		tcwFinancialTransactionTypeItem.setId(id);
		tcwFinancialTransactionTypeItem.setStatus(0);
		tcwFinancialTransactionTypeItemMapper.update(tcwFinancialTransactionTypeItem);
		return R.ok();
	}

}
